package com.bee.gateway.mapper;

import com.bee.gateway.common.domain.RoutesDO;
import com.bee.gateway.common.vo.RouteQueryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Damin.yang
 * @date 2019/07/27 22:58
 */
@Mapper
public interface RoutesMapper {

    String COLUMN = "id, route_id, uri, predicates, filters, orders, status, " +
            "description, create_time, creator, update_time, updator, delete_time";

    /**
     * 插入
     * @param routesDO
     * @return
     */
    @Insert("insert into routes(route_id, uri, predicates, filters, orders, description, create_time, creator, update_time, updator)" +
            " values(#{routeId}, #{uri}, #{predicates}, #{filters}, #{orders}, #{description}, now(), #{creator}, now(), #{updator})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(RoutesDO routesDO);

    /**
     * 删除
     * @param id
     */
    @Update("update routes set status='N' where id=#{id}")
    void delete(long id);

    /**
     * 更新
     * @param routesDO
     */
    @Update("update routes set route_id=#{routeId},uri=#{uri},predicates=#{predicates},filters=#{filters},description=#{description},orders=#{orders},updator=#{updator},update_time=now()" +
            " where id=#{id}")
    void update(RoutesDO routesDO);

    /**
     * 查询一条
     * @param id
     * @return
     */
    @Select("select " + COLUMN + " from routes where id=#{id} and status='Y'")
    RoutesDO get(long id);

    /**
     * 根据条件查询列表
     * @param queryVO
     * @return
     */
    @Select("<script>" +
            "select " + COLUMN +
            " from routes" +
            " where status = 'Y'" +
            "<if test='uri != null'> and uri = #{uri} </if>" +
            "<if test='createdTimeStart != null'> and <![CDATA[created_time > #{createdTimeStart}]]> </if>" +
            "<if test='createdTimeEnd != null'> and <![CDATA[created_time < #{createdTimeEnd}]]> </if>" +
            "</script>")
    List<RoutesDO> query(RouteQueryVO queryVO);
}
